/*
 *Copyright 2021-2023 NERCIS
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
 */

package cn.ac.nercis.pes.community.constant;

import cn.ac.nercis.pes.common.constant.evaluation.EvaluationResult;
import cn.ac.nercis.pes.common.constant.evaluation.PwdRkCondition;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * 密钥管理安全情况测评结果
 * @author zpy
 * @version 1.0.0
 */
public enum EvalRkResult {
    /**默认，未填写**/
    DEFAULT("",EvaluationResult.DEFAULT,PwdRkCondition.DEFAULT),
    MATCH("符合",EvaluationResult.MATCH,PwdRkCondition.DEFAULT),
    NOT_MATCH("不符合",EvaluationResult.NOT_MATCH,PwdRkCondition.DEFAULT),
    RK_TWO_MODULE("不符合/二级密码",EvaluationResult.NOT_MATCH,PwdRkCondition.TWO_LEVEL_MODULE),
    RK_ONE_MODULE("不符合/一级密码",EvaluationResult.NOT_MATCH,PwdRkCondition.ONE_LEVEL_MODULE),
    RK_OTHER_MODULE("不符合/其他情况",EvaluationResult.NOT_MATCH,PwdRkCondition.OTHER_MODULE);

    public static EvalRkResult fromName(String name){
        return Stream.of(EvalRkResult.values())
                .filter(rk-> StringUtils.equals(rk.getName(),name))
                .findFirst()
                .orElse(DEFAULT);
    }

    public static EvalRkResult fromResult(EvaluationResult evalResult,PwdRkCondition rkResult){
        return Stream.of(EvalRkResult.values())
                .filter(item-> Objects.equals(item.result,evalResult)
                        && Objects.equals(item.rkCondition,rkResult))
                .findFirst()
                .orElse(DEFAULT);
    }

    public static List<EvalRkResult> findConditionsByThreeLevel(){
        return List.of(MATCH,NOT_MATCH,RK_ONE_MODULE,RK_OTHER_MODULE);
    }

    public static List<EvalRkResult> findConditionsByFourLevel(){
        return List.of(MATCH,NOT_MATCH,RK_TWO_MODULE,RK_OTHER_MODULE);
    }

    public static List<EvalRkResult> findConditionsByOther(){
        return List.of(MATCH,NOT_MATCH);
    }

    private final String name;
    private final EvaluationResult result;
    private final PwdRkCondition rkCondition;

    EvalRkResult(String name, EvaluationResult result, PwdRkCondition rkCondition) {
        this.name = name;
        this.result = result;
        this.rkCondition = rkCondition;
    }

    public String getName() {
        return name;
    }

    public EvaluationResult getResult() {
        return result;
    }

    public PwdRkCondition getRkCondition() {
        return rkCondition;
    }
}
